package com.ming.demo.easyiot;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.Call;
import okhttp3.MediaType;

import static android.R.id.list;
import static com.zhy.http.okhttp.OkHttpUtils.postString;

/**
 * Created by Administrator on 2018/1/10.
 */

public class PostUtil {
    Context context;
    String updateUrl = "http://14.116.252.204:9080/MsgCallbackGW/sub-callback/apk/pushmsg";

    public PostUtil(Context context) {
        this.context = context;
    }

    public void sumit(JSONArray serviceDatas, final ServiceInfo info) {
        try {
            JSONObject param = new JSONObject();
            param.put("serviceDatas", serviceDatas);
//            param.put("phone", phone);
//            param.put("password", Base64Utils.encode(password));
            postString()
                    .url(updateUrl)
                    .content(param.toString())
                    .mediaType(MediaType.parse("application/json; charset=utf-8"))
                    .build()
                    .execute(new MyCallBack(new Result()) {
                        @Override
                        public void onError(Call call, Exception e) {
                            info.setIsSucceed(1);
                            info.save();
                            Toast.makeText(context, "上报失败！", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(Object response) {
                            Result result = (Result) response;
                            if (result.getOptResult().equals("-1")) {
                                Toast.makeText(context, "上报失败!", Toast.LENGTH_SHORT).show();
                                info.setIsSucceed(1);
                                info.save();
                            } else {
                                Toast.makeText(context, "上报成功", Toast.LENGTH_SHORT).show();
                                info.setIsSucceed(0);
                                info.save();
                                MainActivity activity = (MainActivity) context;
                                activity.setInfo(info);
                            }
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void sumit(JSONArray serviceDatas, final ServiceInfo info, final HistoryAdapter mAdapter, final List<Integer> selectList) {
        try {
            JSONObject param = new JSONObject();
            param.put("serviceDatas", serviceDatas);
//            param.put("phone", phone);
//            param.put("password", Base64Utils.encode(password));
            postString()
                    .url(updateUrl)
                    .content(param.toString())
                    .mediaType(MediaType.parse("application/json; charset=utf-8"))
                    .build()
                    .execute(new MyCallBack(new Result()) {
                        @Override
                        public void onError(Call call, Exception e) {
                            Toast.makeText(context, "上报失败!", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(Object response) {
                            Result result = (Result) response;
                            if (result.getOptResult().equals("-1")) {
                                Toast.makeText(context, "上报失败", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(context, "上报成功", Toast.LENGTH_SHORT).show();
                                Collections.sort(selectList, new Comparator() {
                                            @Override
                                            public int compare(Object o, Object t1) {
                                                int arg0 = (int) o;
                                                int arg1 = (int) t1;
                                                return new Integer(arg1).compareTo(new Integer(arg0));
                                            }
                                        }
                                );
                                for (int i = 0; i < selectList.size(); i++) {
                                    mAdapter.removeList((int) selectList.get(i));
                                    mAdapter.notifyDataSetChanged();
                                }
                                mAdapter.cleanList();
                            }
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public class Result {
        String optResult;

        public String getOptResult() {
            return optResult;
        }

        public void setOptResult(String optResult) {
            this.optResult = optResult;
        }
    }
}
